home *** CD-ROM | disk | FTP | other *** search
- //
- // ctfStdLib.cs
- //
- // Simple capture the flag routines
- //
-
- $maxFlagCount = 5; // no of flags required by a team to end the game
- $flagValue = 25; // points your team gets for capturing
- $carrierValue = 5; // " " " " " killing carrier
- $ctfWon = 0;
-
- //--------------------------------------------------------------------------------
-
- function setDefaultMissionOptions()
- {
- $server::TeamPlay = true;
- $server::AllowDeathmatch = false;
- $server::AllowTeamPlay = true;
- }
-
- //--------------------------------------------------------------------------------
- function onMissionStart()
- {
- initGlobalVars();
- }
-
- function player::onAdd(%this)
- {
- chat(%this, 0, strcat("Welcome to CTF, ", getName(%this), ". Here are the rules:"));
- chat(%this, 0, "- Your flag must be at home for a capture!");
- chat(%this, 0, strcat("- Captures are worth ", $flagValue, " points"));
- chat(%this, 0, strcat("- Flag carriers are worth ", $carrierValue, " points"));
- chat(%this, 0, strcat("- Generic kills are worth ZERO points"));
- chat(%this, 0, strcat("- First team to get ", $maxFlagCount, " flags wins."));
-
- %color = teamToColor(getTeam(%this));
- %colorKey = strcat(%color, "FlagCarried");
- %teamPlayerCount = getTeamPlayerCount(getTeam(%this));
-
- // if no one has players flag, and they are the only player of that color
- // setFlag to true
- if(!dataRetrieve(0,%colorKey) && %teamPlayerCount <= 1)
- {
- setFlag(%color, true );
- }
- }
-
- //--------------------------------------------------------------------------------
- function player::onRemove(%this)
- {
- %teamPlayerCount = getTeamPlayerCount(getTeam(%this));
- if(%teamPlayerCount <= 1)
- {
- %color = teamToColor(getTeam(%this));
- setFlag(%color, false);
- }
- }
-
-
- //--------------------------------------------------------------------------------
- function vehicle::onAdd(%this)
- {
- %color = teamToColor(getTeam(%this));
- %flagKey = strcat(%color, "FlagCount");
-
- // if the flag isn't at the base, but no one has it, correct situation
- if(dataRetrieve(0, %flagKey) && !dataRetrieve(0, strcat(%color, "FlagCarried")))
- {
- setFlag(%color, true);
- }
- }
-
- //--------------------------------------------------------------------------------
- function vehicle::onDestroyed(%destroyed,%destroyer)
- {
- // if the destroyed vehicle has a flag, it goes back to it's base
- %color = dataRetrieve(%destroyed, "hasFlag");
- if (%color != "") {
- playerDropsFlag(%destroyed);
- // let everyone know this guy drops the flag
- wallDim(strcat(getName(%destroyed), " surrenders the ", %color, " flag"));
-
- // destroyer's team gets credit for killing the carrier
- %key = strcat(getTeam(%destroyer), "CarriersKilled");
- dataStore(0, %key, 1 + dataRetrieve(0, %key));
- }
- }
-
- //--------------------------------------------------------------------------------
- function setFlag(%color, %bool)
- {
- %flagCountKey = strcat(%color, "FlagCount");
-
- // set flag to visible
- if(%bool)
- {
- dataStore(0, %flagCountKey, 0);
- setShapeVisibility(getObjectId(strcat("MissionGroup\\Scenario\\", %color, "Base\\", %color, "Flag")), true);
- }
-
- // set flag to non-visible
- else
- {
- dataStore(0, %flagCountKey, 1);
- setShapeVisibility(getObjectId(strcat("MissionGroup\\Scenario\\", %color, "Base\\", %color, "Flag")),false);
- }
- }
-
- function playerDropsFlag(%vehicle)
- {
- // figure out which color of flag the guy is carrying
- %color = dataRetrieve(%vehicle, "hasFlag");
- // the player is no longer carrying this or any flag
- dataRelease(%vehicle, "hasFlag");
- setVehicleSpecialIdentity(%vehicle, false);
- dataStore(0, strcat(%color, "FlagCarried"), 0);
-
- %teamPlayerCount = getTeamPlayerCount(colorToTeam(%color));
- if(%teamPlayerCount < 1)
- {
- setFlag(%color, false);
- }
- else
- {
- setFlag(%color, true);
- }
- }
-
-
- //--------------------------------------------------------------------------------
-
- function checkFlagRetrieved(%atColor, %vehicle)
- {
- // is the game over already?
- if ($ctfWon != 0)
- { return; }
-
- // you made it back to your own base, do you have any other teams' flags?
- if (dataRetrieve(%vehicle, "hasFlag") == "")
- { return; }
-
- // is the player's team flag at his base when he brings back the enemy's flag?
- if (dataRetrieve(0, strcat(%atColor, "FlagCount")) != 0)
- { return; }
-
- // player is no longer carrying the flag
- %hasColor = dataRetrieve(%vehicle, "hasFlag");
- playerDropsFlag(%vehicle);
-
- // player's team gets credit for stealing the flag
- %collectedKey = strcat(getTeam(%vehicle), "FlagsCollected");
- %collectedCount = dataRetrieve(0, %collectedKey);
- %collectedCount = %collectedCount + 1;
- dataStore(0, %collectedKey, %collectedCount);
-
- // let everyone know what happened
- %needMore = $maxFlagCount - %collectedCount;
- if (%needMore == 0) {
- wallDim(strcat(getName(%vehicle), " captured the ", %hasColor, " flag!"));
- $ctfWon = 1;
- schedule("missionEndConditionMet();", 5.0);
- winEvent(getTeam(%vehicle));
- playerDropsFlag(%vehicle);
- }
- else {
- wallDim(strcat(getName(%vehicle), " captured the ", %hasColor, " flag! ", getTeam(%vehicle), " needs ", %needMore, " more to win"));
- }
- }
-
-
- //--------------------------------------------------------------------------------
- function initGlobalVars()
- {
- dataStore(0, "BlueFlagsCollected", 0);
- dataStore(0, "RedFlagsCollected", 0);
- dataStore(0, "YellowFlagsCollected", 0);
- dataStore(0, "PurpleFlagsCollected", 0);
-
- dataStore(0, "blueFlagCount", 1);
- dataStore(0, "redFlagCount", 1);
- dataStore(0, "yellowFlagCount", 1);
- dataStore(0, "purpleFlagCount", 1);
-
- dataStore(0, "yellowFlagCarried", 0);
- dataStore(0, "blueFlagCarried", 0);
- dataStore(0, "redFlagCarried", 0);
- dataStore(0, "purpleFlagCarried", 0);
-
- setShapeVisibility(getObjectId("MissionGroup\\Scenario\\yellowBase\\yellowFlag"), false);
- setShapeVisibility(getObjectId("MissionGroup\\Scenario\\blueBase\\blueFlag"), false);
- setShapeVisibility(getObjectId("MissionGroup\\Scenario\\redBase\\redFlag"), false);
- setShapeVisibility(getObjectId("MissionGroup\\Scenario\\purpleBase\\purpleFlag"), false);
- }
-
-
-
- function winEvent(%Team)
- {
- %r = %g = %b = 0;
-
- if(%Team == Yellow)
- {
- %r = %g = 0.5;
- }
-
- if(%Team == Blue)
- {
- %b = 0.5;
- }
-
- if(%Team == Red)
- {
- %r = 0.5;
- }
-
- if(%Team == Purple)
- {
- %r = 0.345;
- %g = 0.1254;
- %b = 0.254;
- }
-
- %count = playerManager::getPlayerCount();
- for(%i = 0; %i < %count; %i = %i +1)
- {
- %curPlayerNum = playerManager::getPlayerNum(%i);
- messageBox(%curPlayerNum, strcat("GAME OVER! \n", %Team, " won the game!"));
- fadeEvent(%curPlayerNum, out, 6.0, %r, %g, %b);
- }
- }
-
-
- function checkFlagStolen(%color, %vehicle)
- {
- // is the game over already?
- if ($ctfWon != 0)
- { return; }
-
- // you just hit the trigger at an enemy base, is there a flag here?
- %flagCountKey = strcat(%color, "FlagCount");
- %flagCount = dataRetrieve(0, %flagCountKey);
-
- echo(%flagCount);
- if (%flagCount != 0 || dataRetrieve(%vehicle, "hasFlag") != "")
- { return; }
-
- if (getVehicleName(%vehicle) == "none")
- { return; }
-
- // stop the flag shape from rendering
- setShapeVisibility(getObjectId(strcat("MissionGroup\\Scenario\\", %color, "Base\\", %color, "Flag")), false);
-
- // setup the vehicle to carry the flag
- setVehicleSpecialIdentity(%vehicle, true, %color);
- dataStore(%vehicle, "hasFlag", %color);
-
- // increment flag count so nobody else can pickup this flag
- dataStore(0, %flagCountKey, 1);
-
- // set global bool for flag of that color
- dataStore(0, strcat(%color, "FlagCarried"), 1);
-
- // let everyone know what happened
- wallDim(strcat(getName(%vehicle), " STOLE the ", %color, " flag"));
- }
-
- //-------------------------------------------------------------------------------
- function colorToTeam(%color)
- {
- if(%color == "yellow")
- { return "Yellow";}
- if(%color == "blue")
- {return "Blue";}
- if(%color == "red")
- {return "Red";}
- if(%color == "purple")
- {return "Purple";}
-
- return 0;
- }
- function teamToColor(%team)
- {
- if(%team == "Yellow")
- {return "yellow";}
- if(%team == "Blue")
- {return "blue";}
- if(%team == "Red")
- {return "red";}
- if(%team == "Purple")
- {return "purple";}
-
- return 0;
- }
-
- //--------------------------------------------------------------------------------
- function getTeamPlayerCount(%team)
- {
- %count = playerManager::getPlayerCount();
- %teamPlayerCount = 0;
-
- for(%i = 0; %i < %count; %i = %i + 1 )
- {
- %curPlayerNum = playerManager::getPlayerNum(%i);
- if(getTeam(%curPlayerNum) == %team)
- {
- %teamPlayerCount = %teamPlayerCount + 1;
- }
- }
- return %teamPlayerCount;
- }
-
- //--------------------------------------------------------------------------------
- function initFlagTrigger(%name, %color)
- {
- %flagCountKey = strcat(%color, "FlagCount");
- dataStore(0, %flagCountKey, 1);
- %flagsCollectedKey = strcat(%name, "FlagsCollected");
- dataStore(0, %flagsCollected, 0);
- %carriersKilledKey = strcat(%name, "CarriersKilled");
- dataStore(0, %carriersKilledKey, 0);
- }
-
- //--------------------------------------------------------------------------------
- function flagTriggerOnEnter(%colorName, %color, %object)
- {
- %flagKey = strcat(%color, "FlagCount");
- %teamPlayerCount = getTeamPlayerCount(%colorName);
-
- if(%teamPlayerCount < 1 && dataRetrieve(0, %flagKey) != 1)
- {
- %flagKey = strcat(%color, "FlagCount");
- dataStore(0, %flagKey, 1);
- setShapeVisibility(getObjectId(strcat("MissionGroup\\Scenario\\", %color, "Base\\", %color, "Flag")),false);
- wallDim(strcat("The ", %color, " flag vanishes!"));
- }
-
- if (getTeam(%object) != %colorName) {
- checkFlagStolen(%color, %object);
- }
- else {
- checkFlagRetrieved(%color, %object);
- }
- }
-
- //--------------------------------------------------------------------------------
-
- //--------------------------------------------------------------------------------
-
- // Team color specific code
-
- // Yellow
- function yellowFlagTrigger::trigger::onAdd(%this)
- {
- initFlagTrigger("Yellow", "yellow");
- }
- function yellowFlagTrigger::trigger::onEnter(%this, %object)
- {
- flagTriggerOnEnter("Yellow", "yellow", %object);
- }
-
- // Blue
- function blueFlagTrigger::trigger::onAdd(%this)
- {
- initFlagTrigger("Blue", "blue");
- }
- function blueFlagTrigger::trigger::onEnter(%this, %object)
- {
- flagTriggerOnEnter("Blue", "blue", %object);
- }
-
- // Red
- function redFlagTrigger::trigger::onAdd(%this)
- {
- initFlagTrigger("Red", "red");
- }
- function redFlagTrigger::trigger::onEnter(%this, %object)
- {
- flagTriggerOnEnter("Red", "red", %object);
- }
-
- // Purple
- function purpleFlagTrigger::trigger::onAdd(%this)
- {
- initFlagTrigger("Purple", "purple");
- }
- function purpleFlagTrigger::trigger::onEnter(%this, %object)
- {
- flagTriggerOnEnter("Purple", "purple", %object);
- }
-
- //--------------------------------------------------------------------------------
-
- //--------------------------------------------------------------------------------
-
- // Scoreboard code
-
- function getTeamCarriersKilled(%a)
- {
- %key = strcat(getTeamNameFromTeamId(%a), "CarriersKilled");
- return dataRetrieve(0, %key);
- }
-
- function getPlayerCarriersKilled(%a)
- {
- %key = strcat(getTeam(%a), "CarriersKilled");
- return dataRetrieve(0, %key);
- }
-
- function getTeamFlags(%a)
- {
- %collectedKey = strcat(getTeamNameFromTeamId(%a), "FlagsCollected");
- %flags = dataRetrieve(0, %collectedKey);
- if (%flags == "") {
- return 0;
- }
- else {
- return %flags;
- }
- }
-
- function getPlayerFlags(%a)
- {
- %collectedKey = strcat(getTeam(%a), "FlagsCollected");
- %flags = dataRetrieve(0, %collectedKey);
- if (%flags == "") {
- return 0;
- }
- else {
- return %flags;
- }
- }
-
- function getPlayerScore(%a)
- {
- return(getPlayerFlags(%a) * $flagValue + getPlayerCarriersKilled(%a) * $carrierValue);
- }
-
- function getTeamScore(%a)
- {
- return(getTeamFlags(%a) * $flagValue + getTeamCarriersKilled(%a) * $carrierValue);
- }
-
- function initScoreBoard()
- {
- deleteVariables("$ScoreBoard::PlayerColumn*");
- deleteVariables("$ScoreBoard::TeamColumn*");
-
- // Player ScoreBoard column headings
- $ScoreBoard::PlayerColumnHeader1 = "TEAM";
- $ScoreBoard::PlayerColumnHeader2 = "FLAGS";
- $ScoreBoard::PlayerColumnHeader3 = "SCORE";
- $ScoreBoard::PlayerColumnHeader4 = "CARRIER KILLS";
- $ScoreBoard::PlayerColumnHeader5 = "PING";
-
- // Player ScoreBoard column functions
- $ScoreBoard::PlayerColumnFunction1 = "getTeam";
- $ScoreBoard::PlayerColumnFunction2 = "getPlayerFlags";
- $ScoreBoard::PlayerColumnFunction3 = "getPlayerScore";
- $ScoreBoard::PlayerColumnFunction4 = "getPlayerCarriersKilled";
- $ScoreBoard::PlayerColumnFunction5 = "getPing";
-
- //Team ScoreBoard column headings
- $ScoreBoard::TeamColumnHeader1 = "FLAGS";
- $ScoreBoard::TeamColumnHeader2 = "SCORE";
- $ScoreBoard::TeamColumnHeader3 = "CARRIERS KILLED";
-
-
- // Team ScoreBoard column functions
- $ScoreBoard::TeamColumnFunction1 = "getTeamFlags";
- $ScoreBoard::TeamColumnFunction2 = "getTeamScore";
- $ScoreBoard::TeamColumnFunction3 = "getTeamCarriersKilled";
-
- // tell server to process all the scoreboard definitions defined above
- serverInitScoreBoard();
- }
-
- //--------------------------------------------------------------------------------
-
-
-